package arefjell.name.valutakalkulator2;

import android.app.Activity;
import android.widget.AdapterView;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.GridView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity
implements AdapterView.OnItemSelectedListener{
	
	private static final String[] items={"NOK -> EUR","EUR -> NOK",
		"NOK -> USD","USD -> NOK","NOK -> GBP","GBP -> NOK"};
	GridView mKeypadGrid;
	TextView userInputText;
	String numbers ="0";
	
	Spinner s;
	int p = 0;
	KeypadAdapter mKeypadAdapter;
	public void onItemSelected(AdapterView<?> parent,
			View v, int position, long id){p = position;};
			
			public void onNothingSelected(AdapterView<?> parent){};			
			
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	// Fill spinner with exchange rates
	s = (Spinner)findViewById(R.id.spinner);
	s.setOnItemSelectedListener(this);
	ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
			android.R.layout.simple_spinner_item,items);
	aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	s.setAdapter(aa);
	// End of code to fill spinner
	// Get reference to the GridView and TextView
	mKeypadGrid = (GridView) findViewById(R.id.grdButtons);
	userInputText = (TextView) findViewById(R.id.txtInput);
	// Create Keypad Adapter
	mKeypadAdapter = new KeypadAdapter(this);
	// Set adapter of the keypad grid
	mKeypadGrid.setAdapter(mKeypadAdapter);
	// Set button click listener of the keypad adapter
	// Process button clicks
	// Only "veksle" needs special treatment
	mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
		public void onClick(View v) {
			Button btn = (Button) v;
			// Get the KeypadButton value which is used to identify the
			// keypad button from the Button's tag
			KeypadButton keypadButton = (KeypadButton) btn.getTag();
			// Process keypad button
			if (keypadButton.getText().toString() != "Veksle"){
				//userInputText.append(keypadButton.getText().toString());
				numbers = numbers + keypadButton.getText().toString();
			    userInputText.setText(numbers);} 
			else {
			    	double l = Double.parseDouble(numbers);
			    	double o = 0;
			    	switch (p) {
			    		case 0: o = l/ 7.5; break;
			    		case 1: o = l* 7.5; break;
			    		case 2: o = l/ 5.5; break;
			    		case 3: o = l* 5.5; break;
			    		case 4: o = l/ 9.5; break;
			    		case 5: o = l* 9.5; break;
			    	}
			    	numbers = String.valueOf(o);
			    	userInputText.setText(numbers);
			    }
		}
	}
	);}
}
